package com.tjbklx1.thread.chap03.practise06;

import java.util.LinkedList;
import java.util.Queue;

public class RequestQueue {

	private final Queue<Request> queue = new LinkedList<Request>();

	public synchronized Request getRequest() throws InterruptedException {	//注意同步
		while(queue.peek() == null){
				System.out.println(Thread.currentThread().getName()+" : wait() begins,queue = "+queue);
				wait();
				System.out.println(Thread.currentThread().getName()+" : wait() ends,queue = "+queue);
		}
		return queue.remove();
	}

	public synchronized void putRequest(Request request) { //注意同步
		queue.offer(request);
		System.out.println(Thread.currentThread().getName()+" : notifyAll() begins,queue = "+queue);
		notifyAll();
		System.out.println(Thread.currentThread().getName()+" : notifyAll() ends,queue = "+queue);
	}
}
